package com.corpgovernment.common.config;

import com.corpgovernment.common.supplier.DefaultResponseConverter;
import com.corpgovernment.common.supplier.ResponseConverterHolder;
import com.corpgovernment.common.supplier.ResponseConverterRegistrySupport;
import com.corpgovernment.common.supplier.ResultCodeMappingExceptionHolder;
import com.corpgovernment.common.supplier.ResultCodeMappingExceptionRegistrySupport;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 供应商客户端配置
 *
 * @author xukang zhang
 * @date 2024/2/18 14:56
 */
@Configuration
@AllArgsConstructor
public class SupplierClientConfig {


    /**
     * 扩展自定义的ResponseConverter
     */
    @Bean
    public ResponseConverterHolder extendResponseConverter(List<ResponseConverterRegistrySupport> responseConverterRegistrySupportList) {
        ResponseConverterHolder responseConverterHolder = new ResponseConverterHolder();
        if (CollectionUtils.isNotEmpty(responseConverterRegistrySupportList)) {
            responseConverterRegistrySupportList.forEach(responseConverterRegistrySupport -> responseConverterRegistrySupport.extendResponseConverter(responseConverterHolder));
        }
        responseConverterHolder.register(new DefaultResponseConverter(Object.class));
        return responseConverterHolder;
    }

    /**
     * 扩展自定义的resultCode与对应异常的映射关系
     *
     * @param resultCodeMappingExceptionRegistrySupportList
     */
    @Bean
    public ResultCodeMappingExceptionHolder extendResultCodeMappingException(List<ResultCodeMappingExceptionRegistrySupport> resultCodeMappingExceptionRegistrySupportList) {
        ResultCodeMappingExceptionHolder resultCodeMappingExceptionHolder = new ResultCodeMappingExceptionHolder();
        if (CollectionUtils.isNotEmpty(resultCodeMappingExceptionRegistrySupportList)) {
            resultCodeMappingExceptionRegistrySupportList.forEach(responseConverterRegistrySupport -> responseConverterRegistrySupport.extendResultCodeMappingException(resultCodeMappingExceptionHolder));
        }
        return resultCodeMappingExceptionHolder;
    }
}
